package servlet;

import dao.GoodsDao;
import dao.OrderDao;
import dao.UserDao;
import model.Goods;
import model.Order;
import model.User;
import service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(urlPatterns = "/EditOrderServlet.do")
public class EditOrderServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String idParam = req.getParameter("id");
        System.out.println(idParam);
        if (idParam == null || idParam.isEmpty()) {
            resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "User ID is required");
            return;
        }

        try {
            int order_Id = Integer.parseInt(idParam);
            OrderDao orderDao=new OrderDao();

       Order order= orderDao.getOrderByOrderId(order_Id);
            System.out.println(order.getRecipient_name());
            System.out.println(order.getRecipient_phone());
            System.out.println(order.getRecipient_address());
            System.out.println(order.getNotes());
            System.out.println(order.getOrder_status());
            System.out.println(order.getOrder_total());



            if (order != null) {
                HttpSession session = req.getSession();
                session.setAttribute("order", order);
                resp.sendRedirect(req.getContextPath() + "/background1/admin/EidtOrderServlet.jsp");
            } else {
                resp.sendError(HttpServletResponse.SC_NOT_FOUND, "User not found");
            }
        } catch (NumberFormatException e) {
            resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid User ID format");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String idParam = req.getParameter("order_id");
        if (idParam == null || idParam.isEmpty()) {
            resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Order ID is required");
            return;
        }

        try {
            int orderId = Integer.parseInt(idParam);
            String recipientName = req.getParameter("recipient_name");
            String recipientAddress = req.getParameter("recipient_address");
            String recipientPhone = req.getParameter("recipient_phone");
            String notes = req.getParameter("notes");
            String orderStatus = req.getParameter("order_status");
            double orderTotal = Double.parseDouble(req.getParameter("order_total"));

            Order updatedOrder = new Order();
            updatedOrder.setOrder_id(orderId);
            updatedOrder.setRecipient_name(recipientName);
            updatedOrder.setRecipient_address(recipientAddress);
            updatedOrder.setRecipient_phone(recipientPhone);
            updatedOrder.setNotes(notes);
            updatedOrder.setOrder_status(orderStatus);
            updatedOrder.setOrder_total(orderTotal);

            OrderDao orderDao = new OrderDao();
            boolean isUpdated = orderDao.updateOrder(updatedOrder);

            if (isUpdated) {
                resp.sendRedirect(req.getContextPath() + "/background1/admin/orderManager.jsp");
            } else {
                resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Order update failed");
            }
        } catch (NumberFormatException e) {
            resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid order ID format or order total format");
        }
    }



}
